/*!
* Copyright (c) 2011 sxgkwei
* Author:sxgkwei
* Email:sxgkwei@gmail.com
* Date: 2011-11-28 17:10:21
*/
var isIdCard=(function(){
	var maxOld = 100;
	var now = (new Date()).getFullYear();
	
	//判断18位身份证
	var is18IdCard = function(idCard){
		
		var year = parseInt(idCard.substring(6,10),10);
		if(maxOld+year<now){//证明是鬼魂
			return false;
		};
		
		var idCardReg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$/;
		if(!idCardReg.test(idCard)){//基本正则
			return false;
		};
		
		var wi=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];//权列表
		var ex=[1,0,'x',9,8,7,6,5,4,3,2];//校验码列表
		var su=0;
		for(var i=0,wil=wi.length;i<wil;i++){
			su+=parseInt(idCard.charAt(i),10)*wi[i];
		};
		return idCard.charAt(17).toLowerCase()==ex[su%11];
	};
	
	//判断15位身份证
	var is15IdCard = function(idCard){
		
		var maxId = 2003-16;//2003年前是15位证，但办证也得最少16岁吧
		var minId = now-maxOld;
		var year = parseInt("19"+idCard.substring(6,8),10);
		if(minId>maxId || year<minId || year>maxId){//证明是鬼魂或者非人类
			return false;
		};
		
		var idCardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
		
		return	idCardReg.test(idCard);
	};
	
	return (function(e){
		if(e.length==18){
			return is18IdCard(e);
		}else if(e.length==15){
			return is15IdCard(e);
		}else{
			alert("莫乱写，我们不是那么好忽悠滴");
			return false;
		};
	});
})();

isIdCard("410456196511285935");